package com.qf.shrio2203.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.qf.shrio2203.config.CustomRealm;
import com.qf.shrio2203.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class LoginController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    CustomRealm customRealm;
    // @PostMapping("/login")
    public String login(String username, String password) {

        //基于shiro的登录写法
        //subject 代表当前用户信息的对象
        Subject subject = SecurityUtils.getSubject();

        //将用户提供的用户名和密码封装成shiro中需要的usernamepasswordtoken对象
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        //让shiro处理身份验证逻辑
        subject.login(usernamePasswordToken);
        User user = (User) subject.getPrincipal();

        //返回登录成功
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code","1");
//        map.put("msg","ok");
//        return map;
        return "redirect:/home.html";
    }

    @GetMapping("/logout")
    public String logout() {
        //获取当前用户对象
        Subject subject = SecurityUtils.getSubject();
        //注销当前用户
        subject.logout();
        //重定向
        return "redirect:/login.html";
    }
}
